package org.hot100_0.leetcode56;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public static void main(String[] args) {
        int [] [] item ={{1,3},{2,6},{8,10},{15,18}};
        merge(item);
    }
    public static int[][] merge(int[][] intervals) {
        int n1 = intervals.length;
        int n2 = intervals[0].length;

        List<int[]> resList = new ArrayList<>();

        Arrays.sort(intervals, (arr1, arr2) -> arr1[0] - arr2[0]);

        int left = intervals[0][0];
        int right = intervals[0][1];
        for (int[] item : intervals) {
            if (item[0] > right) {
                resList.add(new int[]{left, right});
                left = item[0];
                right = item[1];
            }else {
                right=Math.max(right,item[1]);
            }
        }

        resList.add(new int[]{left, right});

        int [][] res = new int[resList.size()][2];

        for (int i = 0;i<resList.size();i++){
            res[i]=resList.get(i);
        }

        return res;
    }
}
